
package code.codeworld.user.user.service.impl;

import cn.hutool.core.date.DateUtil;
import code.codeworld.user.user.mapper.UserJPA;
import code.codeworld.user.user.mapper.UserMapper;
import code.codeworld.user.user.model.User;
import code.codeworld.user.user.model.vo.UserVo;
import code.codeworld.user.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * UserService接口实现类
 *
 * @author freecode
 * @date 2023/03/23 00:20
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserJPA userJPA;

    @Override
    public Page<User> page(UserVo userVo) {
        Page<User> page = userJPA.findAll(userVo.getPageable());
        page.getContent().forEach(t -> {
            t.setCreateTimeText(DateUtil.format(t.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        });
        return page;
    }

    @Override
    public void add(User user) {
        userJPA.save(user);
    }

    @Override
    public void edit(User user) {
        User theUser = this.queryEntity(user.getId());
        theUser.setNotNull(user);
        userJPA.save(theUser);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<User> UserList) {

        // 执行删除
    }

    @Override
    public User detail(User User) {
        User result = this.queryEntity(User.getId());
        result.setCreateTimeText(DateUtil.format(result.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        return result;
    }

    @Override
    public User queryEntity(Long id) {
        return userJPA.getReferenceById(id.intValue());
    }

}
